import * as THREE from 'three';
import { ParticleSystem } from '../ParticleSystem';
import GUI from "three/examples/jsm/libs/lil-gui.module.min.js";
  const blends = [
    THREE.NormalBlending,
    THREE.AdditiveBlending,
    THREE.SubtractiveBlending,
    THREE.MultiplyBlending,
  ]
export abstract class BaseDemo {
    name: string;
    description: string;
    particleSystem?: ParticleSystem;
    guiFolder?: GUI;
    scene: THREE.Scene;
    
    constructor(name: string, description: string, scene: THREE.Scene) {
        this.name = name;
        this.description = description;
        this.scene = scene;
    }

    /**
     * 初始化demo - 创建粒子系统、发射器等
     */
    abstract init(): void;

    /**
     * 更新demo - 每帧调用
     */
    update(deltaTime: number): void {
        if (this.particleSystem) {
            this.particleSystem.update(deltaTime);
        }
    }

    /**
     * 设置GUI控制面板
     */
    setupGUI(gui: GUI): void {
        if( !this.guiFolder){

            this.guiFolder = gui.addFolder(this.name);
            this.createControls(this.guiFolder);
        }

    }
        initMatGUI(particleMat: THREE.PointsMaterial, gui:GUI): void { 
        const guiOpt={color:particleMat.color.getHex()}
        const matFolder = gui.addFolder('材质')
        matFolder.addColor(guiOpt, 'color').onChange((v)=>{
          particleMat.color.set(v)
        })
        matFolder.add(particleMat, 'blending', blends).onChange((v)=>{
          particleMat.blending = v
        })
        matFolder.add(particleMat, 'transparent')
        matFolder.add(particleMat, 'opacity',0,1)
    
        matFolder.add(particleMat, 'alphaTest',0,1)
        matFolder.add(particleMat, 'sizeAttenuation')
        matFolder.add(particleMat, 'size',1,50,1)
        matFolder.add(particleMat, 'depthTest')
        matFolder.add(particleMat, 'depthWrite')
    }

    /**
     * 创建具体的控制项 - 子类实现
     */
    protected createControls(folder: GUI): void {
        // 子类重写此方法添加自定义控制
    }

    /**
     * 清理资源
     */
    dispose(): void {
        if (this.particleSystem) {
            // 清理粒子系统的渲染对象
            this.particleSystem.emitters.forEach(emitter => {
                this.scene.remove(emitter.points);
                emitter.geometry.dispose();
                emitter.material.dispose();
            });
        }
        
        if (this.guiFolder) {
            this.guiFolder.destroy();
        }
    }

    /**
     * 激活demo
     */
    activate(): void {
        if (this.particleSystem) {
            this.particleSystem.emitters.forEach(emitter => {
                this.scene.add(emitter.points);
            });
        }
    }

    /**
     * 停用demo
     */
    deactivate(): void {
        if (this.particleSystem) {
            this.particleSystem.emitters.forEach(emitter => {
                this.scene.remove(emitter.points);
            });
            this.guiFolder?.hide()
        }
    }

    /**
     * 重置demo
     */
    reset(): void {
        if (this.particleSystem) {
            this.particleSystem.emitters.forEach(emitter => {
                emitter.reLive(emitter.emitterLife);
            });
        }
    }
}